let Controller = {
  mounted() {
    this.initData();
  },
  activated() {
    this.initData();
    this.requestDetail();
  },
  methods: {
    initData() {
      const param = JSON.parse(this.$route.query.data);
      this.Model.queryData = _.cloneDeep(param.row);
      this.Model.detailFlag = Boolean(param.flag);
      this.Model.status = param.row.status;
      this.setStepData(this.Model.status);
      this.Model.filterOption.items[3].type = this.Model.detailFlag ? 'text' : 'dropdown';
      this.onDownloadExcel();
    },
    getFilterData(val) {
      this.Model.searchFromData = val;
		},
		onAddPackingMaterials(){
			const param = {
				"condition":{
					"commodityType":"Z007"
					 }
				};
				
			this.$ajax.post(this.apiTest + `/supplier/whWarehouse/findPackingMaterials`,param)
			.then(res=>{
				const data = res.data;
				if(data.status == 200){
					this.Model.packingMaterialsList = data.result.result;
					this.Model.addDialogFormVisible = true;
				}else{
					this.$message.error(data.msg)
				}
			})
		},
    onDeleteExportData() {
      this.Model.detailData.actualSaleGoodsInfoVos = undefined;
    },
    onDownloadExcel() {
      this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsExcelInfo`)
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            this.Model.fileUrl = data.result.fileUrl;
            this.Model.fileName = data.result.fileName;
          }
        })
    },
    onExportMaterial() {
      this.Model.dialogFormVisible = true;
		},
		onSureAddToPackingGoodsInfoVos(){
			const list = this.Model.packingMaterialsList.filter(item=>{
				return item.mainQuantity > 0;
			});
			
			this.Model.detailData.packingGoodsInfoVos = _.cloneDeep(list);

			this.Model.addDialogFormVisible = false;
		},
    onSureWarehouse() {
      const list = this.Model.detailData.actualSaleGoodsInfoVos;
      if (list && list.length > 0) {
        const data = list[0];
        const data2 = this.Model.detailData.packingGoodsInfoVos.map(item=>{
          return {
            "goodsId":item.id,  //商品id
            "mainQuantity":item.mainQuantity,   //数量
            "isPackageMaterial":1,  //是否包材   0为否  1为是
          }
        })
        const param = {
          "whWarehouseId": this.Model.detailData.whWarehouseId, //售后入库id
          "inWarehouseId": this.Model.filterOption.items[3].value, //入库仓库id
          // "id": this.Model.detailData.shouldSaleGoodsInfoVos[0].whWarehouseDetailId, //应入商品id
          "status": true, //实入状态-写死
          // "barCode": data.batchNo, //条码
          "sourceDetailsId": data.id, //商品id  变成goodsId
					"remark": this.Model.remark, //备注
					poNumberAndIdDtoList:[
						{
              "goodsId":data.id,  //商品id
              "mainQuantity":data.mainQuantity,   //数量
              "isPackageMaterial":0,  //是否包材   0为否  1为是
            },
            ...data2
					]
        };
        this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail`, param)
          .then(res => {
            const data = res.data;
            if (data.status == 200) {
              this.$message({
                type: 'success',
                message: '已成功'
              })
            };
            this.$router.push({
              name: `afterSaleWarehousing`,
            })
          }).catch(err => {

          })
      }
    },
    onTableSelectionChange(val) {
      this.Model.exportTableSelectList = val;
    },
    onUploadSureFile() {
      /*  */
      const file = this.Model.uploadFile;
      let fileName;
      if (file && !file.name) {
        this.$message({
          type: 'error',
          message: '还没有选择要上传的文件!'
        });
        return;
      } else {
        const extension = file.name.split('.')[1] === 'xls';
        const extension2 = file.name.split('.')[1] === 'xlsx';
        const isLt2M = file.size / 1024 / 1024 < 5
        if (!extension && !extension2) {
          this.$message.warning('上传模板只能是 xls、xlsx格式!')
          return;
        }
        fileName = file.name;
      }
      let fileFormData = new FormData();
      fileFormData.append('file', file, fileName); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      let requestConfig = {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
      }
      this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importExcelList`, fileFormData, requestConfig)
        .then(res => {
          const data = res.data;
          if (data.status == 200) {
            this.$message({
              message: '上传成功',
              type: 'success',
            });
            this.Model.detailData.actualSaleGoodsInfoVos = data.result;
            this.Model.dialogFormVisible = false;
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    onUploadFileChange(e) {
      if (e.target.files.length == 0) {
        return;
      }
      this.Model.uploadFile = e.target.files[0];
    },
    cardClick(color) {
      this.Model.cardSelected = color;
    },
    onTabChange(val) {
      switch (val) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          break;
      }
    },
    requestSaleGoodsInfoByEntity() {
      const batchNo = this.Model.exportBarcode;
      const whWarehouseId = this.Model.whWarehouseId;
      this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsInfo?batchNo=${batchNo}&whWarehouseId=${whWarehouseId}`)
        .then(res => {
          const data = res.data;

          if (data.status == 200) {
            const result = data.result;
            this.Model.detailData.actualSaleGoodsInfoVos = [result];
          }
        }).catch(err => {
          console.log(err)
        })
    },
    requestFilterData() {
      //supplierFactoryLocation/listNoPage
      this.$ajax.post(this.apiTest + `/supplier/supplierFactoryLocation/listNoPage`, {})
        .then(response => {
          const data = response.data;
          if (data.status == 200) {
            const result = data.result;
            const opt = result.map(item => {
              return {
                label: `${item.theNameOfThePlace}-${item.descriptionOfWarehouseLocation}`,
                value: item.id
              }
            });
            if (!this.Model.detailFlag) {
              this.$set(this.Model.filterOption.items[3], 'data', opt);
            }
          }
        })
    },
    requestDetail() {
      const param = {
        condition: {
          number: this.Model.queryData.number
        }
      };
      if (param) {
        this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/outAndInDetails`, param)
          .then(response => {
            const data = response.data;
            if (data.status == 200) {
              const result = data.result;
              this.Model.detailData = result;
              this.Model.whWarehouseId = result.whWarehouseId;
              this.Model.remark = result.remark;
              this.Model.number = result.number;
              this.Model.status = result.status;
              this.Model.tabFormInline = [{
                label: '售后入库单',
                value: this.Model.number,
              },
              {
                label: '单据状态',
                value: this.Model.status,
              },
            ];
              this.requestFilterData();
              this.setFilterData();
            }else{
              this.$message.error(data.msg)
            }

          })
          .catch(error => {
            this.$message.error('请求错误');
          });
      }
    },
    setFilterData() {
      const data = this.Model.detailData;
      const flag = this.Model.detailFlag ? 'text' : 'dropdown';
      this.Model.filterOption.items = [{
          label: "入库单类型:",
          type: "text",
          data: data.type,
          placeholder: "请选择",
          value: undefined,
          key: "type"
        },
        {
          label: "售后订单:",
          type: "text",
          data: data.saleNumber,
          placeholder: "请选择",
          value: undefined,
          key: "saleNumber"
        },
        {
          label: "售后单类型:",
          type: "text",
          data: data.orderType,
          placeholder: "顾客姓名/顾客ID/手机号码",
          value: undefined,
          key: "orderType"
        },
        {
          label: "入库仓库:",
          type: flag,
          data: data.inWarehouseName,
          placeholder: "请选择",
          value: undefined,
          key: "inWarehouseId"
        },
        {
          label: "物流公司:",
          type: "text",
          data: data.logisticsCompany,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "logisticsCompany"
        },
        {
          label: "物流单号:",
          type: "text",
          data: data.logisticsNumber,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "logisticsNumber"
        },
        {
          label: "顾客ID:",
          type: "text",
          data: data.customerId,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "customerId"
        },
        {
          label: "顾客姓名:",
          type: "text",
          data: data.manName,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "manName"
        },
        {
          label: "联系方式:",
          type: "text",
          data: data.mobile,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "mobile"
        },
        {
          label: "专属编码:",
          type: "text",
          data: data.exclusiveCode,
          placeholder: "入库单号/专属编码/条码/售后单号",
          value: undefined,
          key: "exclusiveCode"
        },
        {
          label: "作业人员:",
          type: "text",
          data: data.operatorName,
          placeholder: "",
          value: undefined,
          key: "updateBy"
        },
        {
          label: "入库日期:",
          type: "text",
          data: data.sureTime,
          placeholder: "请选择",
          value: undefined,
          key: "sureTime"
        },
        {
          label: "创建人:",
          type: "text",
          data: data.createName,
          placeholder: "请选择",
          value: undefined,
          key: "createBy"
        },
        {
          label: "备注:",
          type: "text",
          data: data.remark,
          placeholder: "请选择",
          value: undefined,
          key: "remark"
        },
      ]
    },
    setStepData(status) {
      switch(status){
        case 0:
          this.Model.stepOption.activeStep = 0;
          break;
        case 10:
          this.Model.stepOption.activeStep = 1;
          break;
        case 20:
          this.Model.stepOption.activeStep = 2;
          break;
        default:
          break;
      }
      
    },
    toBarcodeDetail(row) {
      //跳转条码详情
      const name = 'purchaseWarehousingBarcodeDetailDetail';
      this.$router.push({
        name: name,
        query: {
          data: row
        }
      })
    },
    tirggerFile() {

    },
    uploadFil() {

    }
  }
}
export default Controller;
